<?php

class Hoc_viensController extends BaseController {

    /**
     * Hoc_vien Repository
     *
     * @var Hoc_vien
     */
    protected $hoc_vien;

    public function __construct(Hoc_vien $hoc_vien) {
        $this->hoc_vien = $hoc_vien;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $model_hoc_vien = new Hoc_vien();
        $hoc_viens = $model_hoc_vien->ds_hocvien();

        return View::make('hoc_viens.index', compact('hoc_viens'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        $hoi_dong_thi = Hoi_dong_thi::all();
        $chung_chi = Chung_chi::all();
        $khoa_thi = Khoa_thi::all();
        return View::make('hoc_viens.create')->with(array('hoi_dong_thi' => $hoi_dong_thi, 'chung_chi' => $chung_chi, 'khoa_thi' => $khoa_thi));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $input = Input::all();
        $validation = Validator::make($input, Hoc_vien::$rules);
        if ($validation->passes()) {
            $this->hoc_vien->create($input);

            return Redirect::route('students.index');
        }

        return Redirect::route('students.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $hoc_vien = $this->hoc_vien->findOrFail($id);
        return View::make('hoc_viens.show', compact('hoc_vien'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $hoc_vien = $this->hoc_vien->find($id);
        $khoa_thi = Khoa_thi::all();
        $hoi_dong_thi = Hoi_dong_thi::all();
        $chung_chi = Chung_chi::all();
        if (is_null($hoc_vien)) {
            return Redirect::route('students.index');
        }

        return View::make('hoc_viens.edit', compact('hoc_vien'))
                        ->with('chung_chi', $chung_chi)
                        ->with('khoa_thi', $khoa_thi);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $input = array_except(Input::all(), '_method');
        $input['TK_NhapDiem_Id'] = Auth::user()->id;
        if($input['ket_qua'] == 0){
            $input['so_quyet_dinh_tot_nghiep'] = '';
            $input['so_so_cap_chung_chi'] = '';
            $input['so_hieu_bang'] = '';
        }
        // var_dump($input);
        $validation = Validator::make($input, Hoc_vien::$rules);

        if ($validation->passes()) {
            $hoc_vien = $this->hoc_vien->find($id);
            $hoc_vien->update($input);

            return Redirect::route('students.show', $id);
        }

        return Redirect::route('students.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->hoc_vien->find($id)->delete();

        return Redirect::route('students.index');
    }

}
